package com.building.hcrm.wcf;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.building.hcrm.base.BaseServiceProxy;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.DateDeserializer;
import com.building.hcrm.common.HttpHelper;
import com.building.hcrm.model.DoSignResult;
import com.building.hcrm.model.SignLog;
import com.building.hcrm.model.SignParam;
import com.building.hcrm.model.SignQuery;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.model.SysUserParam;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class CheckInServiceProxy extends BaseServiceProxy
{
	private static CheckInServiceProxy _instance;

	public static CheckInServiceProxy getInstance(Context context)
	{
		if (_instance == null)
		{
			_instance = new CheckInServiceProxy();
		}
		_instance._context = context;
		return _instance;
	}

	/**
	 * 签到
	 * @param param  要求用户账号 、地址 
	 * @return
	 * @throws Exception
	 */
	public DoSignResult DoSign(SignParam param) throws Exception
	{
		DoSignResult re = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "DoSign",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "DoSignResult");
			for(String str : lstRe)
			{
				re = new DoSignResult();
				re = gson.fromJson(str, re.getClass());
			}
		}
		return re;
	}
	
	/**
	 * 获取相关账号签到情况 
	 * @param param 仅需要用户账号即可
	 * @return
	 * @throws Exception
	 */
	public List<SignQuery> GetSignSelfList(SignParam param) throws Exception
	{
		List<SignQuery> re = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "SignSelfList",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "SignSelfListResult");
			for(String str : lstRe)
			{
				re = new ArrayList<SignQuery>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) 
				{
					SignQuery sl = new SignQuery();
					sl = gson.fromJson(ja.getString(i), sl.getClass());
					re.add(sl);
				}
			}
		}
		return re;
	}
	
	/**
	 * 获取相关账号及其下属签到情况 
	 * @param param 仅需要用户账号即可
	 * @return
	 * @throws Exception
	 */
	public List<SignLog> GetSignRangeList(SignParam param) throws Exception
	{
		List<SignLog> re = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "SignRangeList",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "SignRangeListResult");
			for(String str : lstRe)
			{
				re = new ArrayList<SignLog>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) 
				{
					SignLog sl = new SignLog();
					sl = gson.fromJson(ja.getString(i), sl.getClass());
					re.add(sl);
				}
			}
		}
		return re;
	}
}
